// pages/u_ts/u_ts.js
const app = getApp()
var save_order_ts;var save_ts_img;

function uploadimg(data) {
  console.log(data);
  var that = this,
    i = data.i ? data.i : 0,
    success = data.success ? data.success : 0,
    fail = data.fail ? data.fail : 0;
  wx.uploadFile({
    url: data.url,
    filePath: data.path[i],
    name: 'file',
    formData: {
      id: data.id,
      type: data.type
    },
    success: function (res) {
      success++;
      console.log(res)
      console.log(i);
      console.log(data.id);
    },
    fail: function (res) {
      fail++;
      console.log('fail:' + i + "fail:" + fail);
    },
    complete: function (res) {
      console.log(i);
      i++;
      if (i == data.path.length) {   //当图片传完时，停止调用   
        console.log('执行完毕');
        console.log('成功：' + success + " 失败：" + fail);
      } else {
        console.log(i);
        data.i = i;
        data.success = success;
        data.fail = fail;
        uploadimg(data);
      }

    }
  });
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths:[],
    array: ['请选择', '随行物品丢失', '服务态度不好'],
    index:0,
    content:'',
    tempFilePaths:[],
    oid:'',
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  contenttap:function(e){
    this.setData({
      content:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    var aid = wx.getStorageSync('uid');
    save_order_ts = app.data.ucenter_url + 'save_order_ts';
    save_ts_img = app.data.ucenter_url + 'save_ts_img';
    that.setData({
      oid:id
    })
  },
  chooseimage: function (e) {
    var that = this;
    var tempFilePaths = that.data.tempFilePaths;
    var index = e.currentTarget.dataset.index;
    var img = tempFilePaths;
    wx.chooseImage({
      count: 6, // 默认9  
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        for (var i = 0; i < imgsrc.length; i++) {
          img.push(imgsrc[i])
        }
        console.log(img);
        that.setData({
          tempFilePaths: img
        })
      }
    })
  },
  submit:function(e){
    var that = this;
    var oid = that.data.oid;
    var array = that.data.array;
    var index = that.data.index;
    var ids = array[index];
    var content = that.data.content;
    var tempFilePaths = that.data.tempFilePaths;
    if(index == 0){
      wx.showModal({
        title: '提示',
        content: '请选择投诉原因',
        showCancel: false
      })
    }else if(content == ''){
      wx.showModal({
        title: '提示',
        content: '请输入协商内容',
        showCancel: false
      })
    }else{
      var aid = wx.getStorageSync('uid');
      wx.showLoading();
      wx.request({
        url: save_order_ts,
        data: {
          uid:aid,
          oid:oid,
          ids:ids,
          content:content
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          console.log(res.data);
          if(res.data.state == 1){
            var id = res.data.id;
            if(tempFilePaths.length != 0){
              uploadimg({
                url: save_ts_img,//这里是你图片上传的接口
                path: tempFilePaths,//这里是选取的图片的地址数组
                id: id
              });
            }
            setTimeout(function(e){
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: '信息提交成功！',
                showCancel: false,
                success: function (e) {
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              })
            },500)
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})